﻿using UnityEngine;
using System.Collections;
using Base.Custom;
using Base.Extensions;

public class Player : MonoBehaviour
{

    // Use this for initialization
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float sensitivity = 2;
    Vector3 moveDirection;
    Vector3 movement;
    public Camera carem;
    public SingleObject singleobj;
    CharacterController controller;
    void Start()
    {

        moveDirection = Vector3.zero;
        carem = transform.GetChild(0).GetComponent<Camera>();
        //carem.transform.localPosition = new Vector3(0,controller.height / 2, 0);
    }
    // Update is called once per frame
    void Update()
    {

        controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
        }
        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);


        if (Input.GetMouseButtonDown(0))
        {
            if (FreeCamera.freecamera.IsTouchedUI())
                return;
            Ray ray = carem.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000f, LayerMask.GetMask("Mode")))
            {
                if (FreeCamera.freecamera.singleobj != null)
                    FreeCamera.freecamera.singleobj.SetHighlighter();
                FreeCamera.freecamera.singleobj = hit.transform.GetComponent<SingleObject>();
                FreeCamera.freecamera.singleobj.SetHighlighter(Color.yellow);
            }
            else
            {
                if (FreeCamera.freecamera.singleobj != null)
                    FreeCamera.freecamera.singleobj.SetHighlighter();
                FreeCamera.freecamera.singleobj = null;
            }
        }
        if (Input.GetMouseButton(1))
        {
            float CameraX = Input.GetAxis("Mouse X");
            float CameraY = Input.GetAxis("Mouse Y");
            Vector3 Angle = new Vector3(CameraY * 2, -CameraX * 2, 0);
            this.transform.eulerAngles -= Angle;
        }
    }  
}
